#include <bits/stdc++.h>

using namespace std;

const int MAXN = 2010;

typedef long long ll;
typedef struct {int v,w,nxt;}edge;

ll f[MAXN][MAXN];
int n,k,h[MAXN],cnt,siz[MAXN];
edge e[MAXN<<1];

void link(int x,int y,int w){e[++cnt] = edge{y,w,h[x]};h[x] = cnt;}

void dfs(int x,int p){
	siz[x] = 1;
	for(int i = h[x];i;i = e[i].nxt){
		int y = e[i].v; if(y == p) continue;
		dfs(y,x);
		for(int j = 0;j <= min(siz[y],k);j++)
			f[y][j] += (ll)(k-j) * j * e[i].w + (ll)(n-siz[y]-k+j) * (siz[y] - j) * e[i].w;
		for(int j = min(siz[x],k);j >= 0;j--)
			for(int d = min(siz[y],k);d >= 0;d--)
				f[x][j+d] = max(f[x][j+d],f[x][j] + f[y][d]);
		siz[x] += siz[y];
	}
}
		
int main (){
	scanf("%d %d",&n,&k);
	for(int i = 1;i < n;i++){
		int x,y,w;
		scanf("%d %d %d",&x,&y,&w);
		link(x,y,w);
		link(y,x,w);
	}
	dfs(1,0);
	printf("%lld\n",f[1][k]);
	return 0;
}
